package com.java8.exceptionTest;

/**
 * <p>Description: [把异常当做业务逻辑，这性能比较]</p>
 * Created on 2020/5/14
 *
 * @author <a href="mailto: 819712047@qq.com">谢振东 </a>
 * @version 1.0
 */
public class exceptionTest1 {

    /**
     * 将异常当做业务逻辑，这性能比较
     *
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {

        int num = 5;
        while (num <= 50000000) {
            System.out.println("num={" + num + "}");
            // 通过exception错误实现逻辑代码
            test1(num);
            // 不通过exception错误实现逻辑代码
            test2(num);
            num = num * 10;

            Thread.sleep(1000L);
        }
    }

    private static void test2(int num) {
        for (int i = 0; i < 3; i++) {
            long now = System.currentTimeMillis();
            String a = "";
            for (int k = 0; k < num; k++) {
                int b = 1 / 1;
                if (b == 1) {
                    a = (k + 1) + "";
                }
            }
            System.out.println("i={" + (i + 1) + "},耗费： {" + (System.currentTimeMillis() - now) + "}");
        }
    }

    private static void test1(int num) {
        for (int i = 0; i < 3; i++) {
            long now = System.currentTimeMillis();
            String a = "";
            for (int k = 0; k < num; k++) {
                try {
                    throw new Exception("aaaa");
                } catch (Exception ex) {
                    a = (k + 1) + "";
                }
            }
            System.out.println("i={" + (i + 1) + "},耗费： {" + (System.currentTimeMillis() - now) + "}");
        }
    }

}
